home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_02_06
/
2n06076a
< prev
next >
Wrap
Text File
|
1991-04-14
|
998b
|
40 lines
double tonearest(double x)
{
double i, f, dummy;
/* break x into fractional and integral parts */
f = fabs(modf(x,&i));
/* if no fractional part, return integer part */
if (f == 0.0)
return i;
/* check for a fraction of 1/2 */
if (f == 0.5)
{
/* if i isn't even, set i to nearest even */
if (modf(i / 2.0, &dummy) != 0.0)
{
if (x < 0.0)
i -= 1.0;
else
i += 1.0;
}
}
else
{
/* if fraction is greater than 1/2, round i up */
if (f > 0.5)
{
if (x < 0.0)
i -= 1.0;
else
i += 1.0;
}
}
return i;
}